#include "main.h"
#include "air001_bsp.h"
#include "i2c_aht10.h"

#include "AppLayer.h"
#include "ButtonLayer.h"

#include "SoftTimer.h"
// #include "key.h"
#include "key_device.h"
#include "menu.h"

#define SOFT_VERSION      "V1.0.0"

static void SystemInitLayer_Variable(void);

LongKey_Handle_Type stLongKey_Handle[LONG__KEY_MAX];


int main(void)
{
    /* 初始化所有外设，Flash接口，SysTick */
    HAL_Init();

    /* 系统时钟配置 */
    // APP_SystemClockConfig();
    SystemInitLayer_Variable();

    AIR001_BSP_INIT();
    system_data_init();

    #if DEBUG_PRINTF
    printf("******** That is UNIQUE ********\n");
	printf("* Build DATE : ");
	printf(__DATE__);
	printf("\n");
	printf("* Build TIME : ");
	printf(__TIME__);
	printf("\n");
	printf("* Version:");
	printf(" %s \n", SOFT_VERSION);
	printf("*********************************\n");
    #endif

    while (1)
    {
        // 清除看门狗

        SoftTimer_PerformCallback();
    }
}

#define KEY_SCAN_TIME  50 

//===变量初始化
static void SystemInitLayer_Variable(void)
{
    //按键
    LongKeyEvents_Init(&stLongKey_Handle[0], KEY_VAL_ADD, 1000 / KEY_SCAN_TIME, 100 / KEY_SCAN_TIME);	   
	LongKeyEvents_Init(&stLongKey_Handle[1], KEY_VAL_OK, 2000 / KEY_SCAN_TIME, 0);			
	LongKeyEvents_Init(&stLongKey_Handle[2], KEY_VAL_SUB, 1000 / KEY_SCAN_TIME, 100 / KEY_SCAN_TIME); 
	LongKeyEvents_Init(&stLongKey_Handle[3], KEY_VAL_RESET, 1000 / KEY_SCAN_TIME, 1000 / KEY_SCAN_TIME);
    LongKeyEvents_Init(&stLongKey_Handle[4], KEY_VAL_DEBUG, 1500 / KEY_SCAN_TIME, 1000 / KEY_SCAN_TIME);

    //长按按键事件注册----按键句柄-------------按下回调---松手回调---长按开始回调----长按保持回调-------------------说明
    LongKeyEvents_Set_CB(&stLongKey_Handle[0], NULL, SlideDownKey_Short_CB, NULL, SlideDownKey_LongHold_CB);	
	LongKeyEvents_Set_CB(&stLongKey_Handle[1], NULL, PowerKey_Short_CB, PowerKey_LongStart_CB, NULL);		
    LongKeyEvents_Set_CB(&stLongKey_Handle[2], NULL, SlideUpKey_Short_CB, NULL, SlideUpKey_LongHold_CB);
    LongKeyEvents_Set_CB(&stLongKey_Handle[3], NULL, SlideResetKey_Short_CB, NULL, NULL);	
    LongKeyEvents_Set_CB(&stLongKey_Handle[4], NULL, NULL, SlideDebugKey_LongHold_CB, NULL);	
    
    // 菜单数据初始化
    menu_init();

    //软件定时器
    SoftTimer_Init();										  //初始化软件定时器
	SoftTimer_Create(KEY_SCAN_TIME / 1, Key_Analysis_50MS);	  // 50MS----按键解析
	SoftTimer_Create(100 / 1, SoftwareTimer_100MS);			  // 50MS----软件定时器
	SoftTimer_Create(1000 / 1, SoftwareTimer_1000MS);		  // 1000MS--软件定时器
}

/**
 * @brief   错误执行函数
 * @param   无
 * @retval  无
 */
void Error_Handler(void)
{
    /* 无限循环 */
    while (1)
    {
    }
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  输出产生??言错???的源文件名及行号
 * @param  file：源文件名指??
 * @param  line：发生断言错???的???号
 * @retval ??
 */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* 用户??以根??需要添加自己的打印信息,
       例???: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* 无限???? */
    while (1)
    {
    }
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT AirM2M *****END OF FILE******************/
